package com.mmf.ancientcostume.other.zhy.imageloader;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.mmf.ancientcostume.R;
import com.mmf.ancientcostume.other.zhy.utils.CommonAdapter;
import com.mmf.ancientcostume.other.zhy.utils.ViewHolder;
import com.mmf.ancientcostume.widget.JumpPreviewActivityListener;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends CommonAdapter<String> {

    private int maxNum = 0;
    /**
     * 用户选择的图片，存储为图片的完整路径
     */
    public List<String> mSelectedImage = new ArrayList<>();
    private List<String> dirAllPath = new ArrayList<>();
    private JumpPreviewActivityListener jPAListener;

    /**
     * 文件夹路径
     */
    private String mDirPath;

    public MyAdapter(Context context, List<String> mDatas, int itemLayoutId,
                     String dirPath, int maxNum) {
        super(context, mDatas, itemLayoutId);
        this.mDirPath = dirPath;
        for (String path : mDatas) {
            dirAllPath.add(mDirPath + "/" + path);
        }
        this.maxNum = maxNum;
    }

    @Override
    public void convert(ViewHolder helper, final String item, final int position) {
        //设置no_pic
        helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
        //设置no_selected
        helper.setImageResource(R.id.id_item_select,
                R.drawable.picture_unselected);
        //设置图片
        helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item);

        final ImageView mImageView = helper.getView(R.id.id_item_image);
        final ImageView mSelect = helper.getView(R.id.id_item_select);

        mImageView.setColorFilter(null);
        //设置ImageView的点击事件
        mSelect.setOnClickListener(new OnClickListener() {
            //选择，则将图片变暗，反之则反之
            @Override
            public void onClick(View v) {
                // 已经选择过该图片
                if (mSelectedImage != null && mSelectedImage.contains(mDirPath + "/" + item)) {
                    mSelectedImage.remove(mDirPath + "/" + item);
                    mSelect.setImageResource(R.drawable.picture_unselected);
                    mImageView.setColorFilter(null);
                } else
                // 未选择该图片
                {
                    //max == 0 时表示不限制添加的数量
                    // 如果是添加图片 当限制添加数量且可添加的数量不超过则允许添加  否则不添加
                    if (maxNum != 0) {
                        if (mSelectedImage.size() < maxNum) {
                            mSelectedImage.add(mDirPath + "/" + item);
                            mSelect.setImageResource(R.drawable.pictures_selected);
                            mImageView.setColorFilter(Color.parseColor("#77000000"));
                        } else {
                            Toast.makeText(mContext, "所选的数量不能超过" + maxNum, Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        mSelectedImage.add(mDirPath + "/" + item);
                        mSelect.setImageResource(R.drawable.pictures_selected);
                        mImageView.setColorFilter(Color.parseColor("#77000000"));
                    }
                }
            }
        });
        //设置ImageView的点击事件
        mImageView.setOnClickListener(new OnClickListener() {
            //选择，则将图片变暗，反之则反之
            @Override
            public void onClick(View v) {
                jPAListener.jump(position, dirAllPath);
            }
        });
        //已经选择过的图片，显示出选择过的效果
        if (mSelectedImage != null && mSelectedImage.contains(mDirPath + "/" + item)) {
            mSelect.setImageResource(R.drawable.pictures_selected);
            mImageView.setColorFilter(Color.parseColor("#77000000"));
        }

    }

    public void setjPAListener(JumpPreviewActivityListener jPAListener) {
        this.jPAListener = jPAListener;
    }
}
